package com.example.administrator.trains;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class show extends Activity {
    String user_id;
    String should_show_trian_type;
    List<Map<String,String>> list2 = new ArrayList<Map<String, String>>();
    List<Map<String,String>> list3;
    List<Map<String,String>> list1 = new ArrayList<Map<String, String>>();

    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                ListView lv = (ListView) findViewById(R.id.lv);
                for (int i = 0;i < list2.size();i++) {
                    String type = list2.get(i).get("type");
                    if (should_show_trian_type.equals(type)) {
                        list1.add(list2.get(i));
                    }
                }
                if (should_show_trian_type.equals("all")){
                    ListAdapter la = new SimpleAdapter(show.this, list2,
                            R.layout.activity_show_list_item,
                            new String[]{"type1", "type2", "type3", "type4",
                                    "vehicleNum", "startPlace", "endPlace",
                                    "startTime", "endTime", "type", "show_type1_num",
                                    "show_type2_num", "show_type3_num", "show_type4_num",
                                    "show_type1_price", "show_type2_price", "show_type3_price",
                                    "show_type4_price"},
                            new int[]{R.id.type1, R.id.type2, R.id.type3, R.id.type4, R.id.vehicleNum,
                                    R.id.startPlace, R.id.endPlace,
                                    R.id.information, R.id.endTime,
                                    R.id.type, R.id.num1, R.id.num2,
                                    R.id.num3, R.id.num4, R.id.price1,
                                    R.id.price2, R.id.price3, R.id.price4});
                    lv.setAdapter(la);
                } else {
                    ListAdapter la = new SimpleAdapter(show.this, list1,
                            R.layout.activity_show_list_item,
                            new String[]{"type1", "type2", "type3", "type4", "vehicleNum", "startPlace", "endPlace",
                                    "startTime", "endTime", "type", "show_type1_num",
                                    "show_type2_num", "show_type3_num", "show_type4_num",
                                    "show_type1_price", "show_type2_price", "show_type3_price",
                                    "show_type4_price"},
                            new int[]{R.id.type1, R.id.type2, R.id.type3, R.id.type4, R.id.vehicleNum,
                                    R.id.startPlace, R.id.endPlace,
                                    R.id.information, R.id.endTime,
                                    R.id.type, R.id.num1, R.id.num2,
                                    R.id.num3, R.id.num4, R.id.price1,
                                    R.id.price2, R.id.price3, R.id.price4});
                    lv.setAdapter(la);
                }

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent i = new Intent(show.this, order.class);

                        if (should_show_trian_type.equals("all")) {
                            i.putExtra("id", user_id);
                            i.putExtra("vehicleNum", list2.get(position).get("vehicleNum").toString());
                            i.putExtra("startPlace", list2.get(position).get("startPlace").toString());
                            i.putExtra("endPlace", list2.get(position).get("endPlace").toString());
                            i.putExtra("startTime", list2.get(position).get("startTime").toString());
                            i.putExtra("endTime", list2.get(position).get("endTime").toString());
                            i.putExtra("date", list2.get(position).get("date").toString());
                            i.putExtra("type", list2.get(position).get("type").toString());
                            i.putExtra("type1_num", list2.get(position).get("type1_num").toString());
                            i.putExtra("type2_num", list2.get(position).get("type2_num").toString());
                            i.putExtra("type3_num", list2.get(position).get("type3_num").toString());
                            i.putExtra("type4_num", list2.get(position).get("type4_num").toString());
                            i.putExtra("type1_price", list2.get(position).get("type1_price").toString());
                            i.putExtra("type2_price", list2.get(position).get("type2_price").toString());
                            i.putExtra("type3_price", list2.get(position).get("type3_price").toString());
                            i.putExtra("type4_price", list2.get(position).get("type4_price").toString());

                            startActivity(i);
                        } else {
                            i.putExtra("id", user_id);
                            i.putExtra("vehicleNum", list1.get(position).get("vehicleNum").toString());
                            i.putExtra("startPlace", list1.get(position).get("startPlace").toString());
                            i.putExtra("endPlace", list1.get(position).get("endPlace").toString());
                            i.putExtra("startTime", list1.get(position).get("startTime").toString());
                            i.putExtra("endTime", list1.get(position).get("endTime").toString());
                            i.putExtra("date", list1.get(position).get("date").toString());
                            i.putExtra("type", list1.get(position).get("type").toString());
                            i.putExtra("type1_num", list1.get(position).get("type1_num").toString());
                            i.putExtra("type2_num", list1.get(position).get("type2_num").toString());
                            i.putExtra("type3_num", list1.get(position).get("type3_num").toString());
                            i.putExtra("type4_num", list1.get(position).get("type4_num").toString());
                            i.putExtra("type1_price", list1.get(position).get("type1_price").toString());
                            i.putExtra("type2_price", list1.get(position).get("type2_price").toString());
                            i.putExtra("type3_price", list1.get(position).get("type3_price").toString());
                            i.putExtra("type4_price", list1.get(position).get("type4_price").toString());

                            startActivity(i);
                        }
                    }
                });}

            if (msg.what==2) {
                ListView lv = (ListView) findViewById(R.id.lv);
                ListAdapter la = new SimpleAdapter(show.this,list3,R.layout.activity_show_list_item2,new String[]{"information"},new int[]{R.id.information});
                lv.setAdapter(la);
            }
            if (msg.what==3) {
                Toast.makeText(show.this, "没有该车票", Toast.LENGTH_LONG).show();
            }
          }
    };

    private byte one_data;
    private int k = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.activity_show);

        Intent i = getIntent();
        user_id = i.getStringExtra("id");
        should_show_trian_type = i.getStringExtra("type");

        new Thread(){
            @Override
            public void run() {
                super.run();
                byte[] buffer = new byte[20000];
                int j = 1;
                byte[] one_ticket = new byte[30];
                byte[] from_station = new byte[30];
                byte[] to_station = new byte[30];
                byte[] train_number = new byte[10];
                byte[] departure_time = new byte[10];
                byte[] arrival_time = new byte[10];
                byte[] type1_price = new byte[5];
                byte[] type2_price = new byte[5];
                byte[] type3_price = new byte[5];
                byte[] type4_price = new byte[5];
                byte[] to_date = new byte[11];
                byte[] type1_num = new byte[5];
                byte[] type2_num = new byte[5];
                byte[] type3_num = new byte[5];
                byte[] type4_num = new byte[5];
                byte[] train_type = new byte[4];

                try {
                    Socket.connector.rec_data(buffer);
                    Socket.connector.send(Socket.connector.hexStringToByteArray(Socket.connector.ACK_OK));
                    if (buffer[0] == 7) {
                        Message m = new Message();
                        m.what=3;
                        myHandler.sendMessage(m);

                        return;
                    }
                } catch (IOException e) {
                }

                for (int i = 7; i > 0; i++) {
                    one_data = buffer[i];
                    if (one_data == '|') {
                        switch (j) {
                            case 1:
                                from_station = one_ticket.clone();
                                break;
                            case 2:
                                to_station = one_ticket.clone();
                                break;
                            case 3:
                                departure_time = one_ticket.clone();
                                break;
                            case 4:
                                arrival_time = one_ticket.clone();
                                break;
                            case 5:
                                train_number = one_ticket.clone();
                                train_type[0] = train_number[0];
                                break;
                            case 6:
                                type1_price = one_ticket.clone();
                                break;
                            case 7:
                                type2_price = one_ticket.clone();
                                break;
                            case 8:
                                type3_price = one_ticket.clone();
                                break;
                            case 9:
                                type4_price = one_ticket.clone();
                                break;
                            case 10:
                                to_date = one_ticket.clone();
                                break;
                            case 11:
                                type1_num = one_ticket.clone();
                                break;
                            case 12:
                                type2_num = one_ticket.clone();
                                break;
                            case 13:
                                type3_num = one_ticket.clone();
                                break;
                            case 14:
                                type4_num = one_ticket.clone();
                                break;
                        }
                        while (k >= 0) {
                            one_ticket[k] = 0;
                            k--;
                        }
                        k = 0;
                        j++;
                        continue;
                    }
                    /*读完一条车票信息，加入map中*/
                    if (one_data == ',') {
                        String str_train_number = new String(train_number).trim();
                        String str_from_station = new String(from_station).trim();
                        String str_to_station = new String(to_station).trim();
                        String str_departure_time = new String(departure_time).trim();
                        String str_arrival_time = new String(arrival_time).trim();
                        String str_to_data = new String(to_date).trim();
                        String str_type = new String(train_type);
                        String str_type1_price = new String(type1_price).trim();
                        String str_type2_price = new String(type2_price).trim();
                        String str_type3_price = new String(type3_price).trim();
                        String str_type4_price = new String(type4_price).trim();
                        String str_type1_num = new String(type1_num).trim();
                        String str_type2_num = new String(type2_num).trim();
                        String str_type3_num = new String(type3_num).trim();
                        String str_type4_num = new String(type4_num).trim();

                        Map<String, String> map = new HashMap<String, String>();
                        map.put("vehicleNum", str_train_number);
                        map.put("startPlace", str_from_station);
                        map.put("endPlace", str_to_station);
                        map.put("startTime", str_departure_time);
                        map.put("endTime", str_arrival_time);
                        map.put("date", str_to_data);
                        map.put("type", str_type.trim());
                        map.put("type1_num", str_type1_num);
                        map.put("type2_num", str_type2_num);
                        map.put("type3_num", str_type3_num);
                        map.put("type4_num", str_type4_num);
                        map.put("show_type1_num", str_type1_num + "张");
                        map.put("show_type2_num", str_type2_num + "张");
                        map.put("show_type3_num", str_type3_num + "张");
                        map.put("type1_price", str_type1_price);
                        map.put("type2_price", str_type2_price);
                        map.put("type3_price", str_type3_price);
                        map.put("type4_price", str_type4_price);
                        map.put("show_type1_price", str_type1_price + "元");
                        map.put("show_type2_price", str_type2_price + "元");
                        map.put("show_type3_price", str_type3_price + "元");

                        if (!str_type4_num.trim().equals("无")) {
                            map.put("show_type4_num", str_type4_num + "张");
                            map.put("show_type4_price", str_type4_price + "元");
                        } else {
                            map.put("show_type4_num", str_type4_num);
                            map.put("show_type4_price", str_type4_price);
                        }
                        if (str_type.trim().equals("G")){
                            map.put("type1","二等座:");
                            map.put("type2","一等座:");
                            map.put("type3","特等座:");
                            map.put("type4","无:");
                        }
                        if (str_type.trim().equals("C") ||
                                str_type.trim().equals("D")){
                            map.put("type1","无座:");
                            map.put("type2","二等座:");
                            map.put("type3","一等座:");
                            map.put("type4","无:");
                        }
                        if (str_type.trim().equals("T")||
                                str_type.trim().equals("K") ||
                                str_type.trim().equals("Z")){
                            map.put("type1","无座:");
                            map.put("type2","硬座:");
                            map.put("type3","硬卧:");
                            map.put("type4","软卧:");
                        }

                        list2.add(map);

                        k = 0;
                        j = 1;
                        continue;
                    }
                    if (one_data == '.') {
                        break;
                    }
                    one_ticket[k] = one_data;
                    k++;
                }

                Message m = new Message();
                m.what=1;
                myHandler.sendMessage(m);
            }
        }.start();
    }
}

